import { htmlToText } from 'html-to-text'
import { domain } from './config/index.js'

function getSubTechniques($) {
  // 提取子技术
  const subTechniques = []
  $('#subtechniques-card-body tr').each(function () {
    const id = $(this).find('a').eq(0).attr('data-subtechnique_id')
    const name = $(this).find('td').eq(1).text().trim()
    const url = domain + $(this).find('a').eq(0).attr('href')
    if (id && id != '') subTechniques.push({ id, name, url })
  })

  return subTechniques
}

class Page {
  getContentJson($) {
    // 提取标题
    // const title = $('.jumbotron h1').text().trim()

    const subTechniques = getSubTechniques($)

    // 提取过程示例
    const procedureExamples = []
    $('#examples')
      .next('div')
      .find('tbody tr')
      .each(function () {
        const id = $(this).find('td:first-child a').text().trim()
        const name = $(this).find('td:nth-child(2) a').text().trim()
        const url = domain + $(this).find('td:nth-child(2) a').attr('href')
        const description = $(this).find('td:nth-child(3) p').text().trim()
        const reference = {
          text: $(this).find('td:nth-child(3) span.scite-citeref-number').text().trim(),
          url: $(this).find('td:nth-child(3) span.scite-citeref-number a').attr('href'),
        }
        procedureExamples.push({ id, name, url, description, reference })
      })

    // 提取描述
    const description = $('.description-body').text().trim()
    const descriptionHtml = $('.description-body').html()

    // 提取详细信息
    const details = {
      id: $('.card-data#card-id').text().replace('ID:', '').trim(),
      tactic: $('.card-data#card-tactics a').text().trim(),
      // platforms: $(".card-data:contains('Platforms:')").text().replace('Platforms:', '').trim(),
      platforms: htmlToText($(".card-data:contains('Platforms:') .card-title").html()),
      version: $(".card-data:contains('Version:')").text().replace('Version:', '').trim(),
      created: $(".card-data:contains('Created:')").text().replace('Created:', '').trim(),
      lastModified: $(".card-data:contains('Last Modified:')").text().replace('Last Modified:', '').trim(),
    }

    // 提取缓解措施
    const mitigations = []
    $('#mitigations')
      .next('div')
      .find('tr')
      .each(function () {
        const id = $(this).find('td:first-child a').text().trim()
        const name = $(this).find('td:nth-child(2) a').text().trim()
        const url = domain + $(this).find('td:nth-child(2) a').attr('href')
        const description = $(this).find('td:nth-child(3) p').text().trim()
        if (id && id != '') mitigations.push({ id, name, url, description })
      })

    // 提取检测方法
    const detections = []
    $('.datasources-table tbody tr').each(function () {
      const index = $(this).index()
      let id = $(this).find('td:first-child a').text().trim()
      let url = domain + $(this).find('td:first-child a').attr('href')
      let dataSource = $(this).find('td:nth-child(2) a').text().trim()
      let dataComponent = $(this).find('td:nth-child(3) a').text().trim()
      let description = $(this).find('td:nth-child(4) p').text().trim()

      // 处理表格合并
      if (id == 0 && index > 0) {
        const prev = detections[index - 1]
        if (id == '') {
          id = prev.id
          url = prev.url
        }
        if (dataSource == '') dataSource = prev.dataSource
      }

      detections.push({ id, dataSource, url, dataComponent, description })
    })

    // 提取参考文献
    const references = []
    $('.scite-citation').each(function () {
      const id = $(this).attr('id')
      const text = $(this).find('.scite-citation-text').text().trim()
      const url = $(this).find('.scite-citation-text a').attr('href')
      references.push({ id, text, url })
    })

    return {
      subTechniques,
      procedureExamples,
      description,
      descriptionHtml,
      details,
      mitigations,
      detections,
      references,
    }
  }
}

export default Page
